<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    //$time:传递的时间戳
	//这个函数用来知道一个时间发布距今有多久
	public function time_format($time){
		//格式化发布时间
		$time_format=(date('Y-m-d',$time));
		//当前时间戳 ，距1970年后的秒数second
		$now=time();
		//当天的零时零分
		//strtotime函数用来所英文日期变成时间戳
		$today=strtotime(date('y-m-d',$now));
		//时间差
		$diff=$now-$time;
		//时间差数据
		$str='';
		switch ($diff) {
			//一分钟以内,输出秒
			case $diff<60:
				$str=$diff.'秒前';
				break;
			//一小时以内,输出分钟
			case $diff<3600:
				//向下取整，分钟不要小数
				$str=floor($diff/60).'分钟前';
				break;
			//8小时以内
			case $diff<(3600*8):
				$str=floor($diff/3600).'小时前';
				break;
			//在发布微博时间的当天输出当天发布时间
			case $time>$today:
				$str='今天';
				break;
			//默认输出日期
			default:
				$str=$time_format; 	
		}
		return $str;
	}
}
